package cn.myzf.ftp.cogradient.netty.progressbars;

import io.netty.channel.Channel;
import io.netty.channel.ChannelProgressiveFuture;
import io.netty.channel.ChannelProgressiveFutureListener;
import org.apache.commons.net.ftp.FTPClient;

/**
 * 下载进度回调
 *
 * @author bwcx_jzy
 * @date 2019/12/28
 */
public class Progress implements ChannelProgressiveFutureListener {
    private Channel channel;
    private String reqId;

    private FTPClient ftpClient;

    private long totalSize;

    public Progress(Channel channel, FTPClient ftpClient, long totalSize) {
        this.channel = channel;
        this.ftpClient = ftpClient;
        this.totalSize = totalSize;
    }

    @Override
    public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {
        String progressStr = String.format("%.2f", (progress / (double) totalSize) * 100);
        System.out.println(progressStr);
    }

    @Override
    public void operationComplete(ChannelProgressiveFuture future) throws Exception {
     if(ftpClient.completePendingCommand()){
         ftpClient.logout();
         ftpClient.disconnect();
     }
    }

}
